package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.SRole;
import com.tmeone.erpone.service.ISRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-16
 */
@RestController
public class SRoleController {


    @Autowired
    private ISRoleService sRoleService;

    @ResponseBody
    @RequestMapping("/roles")
    public IPage<SRole> selectListRole(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current) {
        try {
            IPage<SRole> arg = new Page<>(current, 5);
            IPage<SRole> page = sRoleService.pageSRole(arg);
            return page;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/role", method = RequestMethod.PUT)
    public void updateRole(@RequestBody SRole role) {
        role.setRoleUpdateDate(LocalDateTime.now());
        sRoleService.updateById(role);
        System.out.println("更新" + role.toString());

    }

    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public void insertRole(@RequestBody SRole role) {
        role.setRoleCreateDate(LocalDateTime.now());
        sRoleService.save(role);
        System.out.println("添加" + role.toString());
    }

    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public void deleteRole(@PathVariable String id) {
        sRoleService.removeById(id);
        System.out.println("删除：" + id);
    }

    @ResponseBody
    @RequestMapping("/roleuser")
    public List<SRole> selectRole() {
        return sRoleService.list();
    }


}
